package com.tjetc.impl;

import com.tjetc.dao.CommunitycomplaintMapper;
import com.tjetc.domian.Communitycomplaint;
import com.tjetc.service.ComplaintService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class ComplaintServiceImpl implements ComplaintService {
    private CommunitycomplaintMapper complaintMapper = Db.getMapper(CommunitycomplaintMapper.class);
    @Override
    public List<Communitycomplaint> select(int residentId) {
        return complaintMapper.select(residentId);
    }
    @Override
    public Result add(Communitycomplaint communitycomplaint) {
        try {
            Db.start();
            if (communitycomplaint.getResidentid() != null) {
                List<Communitycomplaint> existingComplaints = complaintMapper.selectByRid(communitycomplaint.getResidentid());
                if (!existingComplaints.isEmpty()) {
                    for (Communitycomplaint existingComplaint : existingComplaints) {
                        if (communitycomplaint.equals(existingComplaint)) {

                            return new Result(500, "该用户已经投诉过", null);
                        }
                    }
                }
                int rows = complaintMapper.insertSelective(communitycomplaint);
                Db.commit();
                return rows == 1 ? new Result(200, "添加成功", communitycomplaint) : new Result(500, "添加失败", null);
            }
        } catch (Exception e) {
            Db.rollback();
            throw new RuntimeException(e);
        } finally {
            Db.close();
        }
        return new Result(500, "ResidentID不能为空", null);
    }

    @Override
    public Result update(Communitycomplaint communitycomplaint) {
        if (communitycomplaint.getComplaintid() != null) {
            int rows = complaintMapper.updateByPrimaryKeySelective(communitycomplaint);
            return rows == 1 ? new Result(200, "修改成功", communitycomplaint) : new Result(500, "修改失败", null);
        }
        return new Result(500, "ComplaintID不能为空", null);
    }

    @Override
    public Result delete(int complaintid) {
        //查有没有
        try {
            Communitycomplaint complaint = complaintMapper.selectByPrimaryKey(complaintid);
            if (complaint == null) {
                return new Result(500, "该投诉不存在", null);
            }
            int rows = complaintMapper.deleteByPrimaryKey(complaintid);
            return rows == 1 ? new Result(200, "删除成功", null) : new Result(500, "删除失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(500, "删除失败", null);
    }
    @Override
    public List<Communitycomplaint> selectAll() {
        return complaintMapper.selectAll();
    }

    @Override
    public Result update(int complaintid, String status) {
        try {
            Db.start();
            if (complaintid != 0) {
                Communitycomplaint communitycomplaint = complaintMapper.selectByPrimaryKey(complaintid);
                if (communitycomplaint != null) {
                    communitycomplaint.setStatus(status);
                    int rows = complaintMapper.updateBycomplainId(communitycomplaint);
                    Db.commit();
                    return rows == 1 ? new Result(200, "修改成功", communitycomplaint) : new Result(500, "修改失败", null);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return new Result(500, "修改失败", null);
    }
}
